package com.example.dishit.view.main;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dishit.R;
import com.example.dishit.controller.main.MainController;
import com.example.dishit.model.dish.DishData;
import com.example.dishit.view.recommendation.RecommendationActivity;

public class DishArrayAdapter extends ArrayAdapter<DishData> {
    private final Context context;
    private final List<DishData> dishes;
    
    public DishArrayAdapter(Context context, List<DishData> dishes) {
        super(context, R.layout.dishdata, dishes);
        this.context = context;
        this.dishes = dishes;
    }
    
    private final Executor imageRetrievalThread = Executors.newSingleThreadExecutor();
    
    @Override
    public View getView(int position, final View convertView, final ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final DishData dish = dishes.get(position);
        final View dishView = inflater.inflate(R.layout.dishdata, parent, false);
        
        // IMAGE
        imageRetrievalThread.execute(new Runnable() {
            public void run() {
                final ImageView imageView = (ImageView) dishView.findViewById(R.id.icon);
                if(dish.getImageUrls() != null && dish.getImageUrls().size() > 0) {
                    final String url = dish.getImageUrls().get(0);
                    final Drawable img = loadImageFromURL(url);
                    parent.post(new Runnable() {
                        public void run() {
                            imageView.setImageDrawable(img);
                        }
                    });
                }
            }
        });
        
        // TEXT
        String recommendedBy = dish.getRecommendedBy();
        recommendedBy = (recommendedBy != null && !recommendedBy.isEmpty()) ? recommendedBy : "Unknown";
        final String text = new StringBuilder()
                .append(recommendedBy).append(":\n\t")
                .append(dish.getRestaurantName()).append("    (")
                .append(new Date(dish.getTimeOfConstructionMillis())).append(")")
                .toString();
        TextView textView = (TextView) dishView.findViewById(R.id.text);
        textView.setText(text);
        
        // Add click-listener for the row.
        dishView.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                MainController.instance().transition(dish, RecommendationActivity.class);
            }
        });
        
        return dishView;
    }
    
    private Drawable loadImageFromURL(String url) {
        Drawable drawable = null;
        try {
            final InputStream is = (InputStream) new URL(url).getContent();
            drawable = Drawable.createFromStream(is, "srcname");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return drawable;
        // return getContext().getResources().getDrawable(R.drawable.cutters);
    }
}
